package com.binge.thread;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * ClassName:TestLock
 * Package:com.binge.thread
 * Description:
 *
 * @Author @Bin
 * @Date 2024/9/19-14:17
 * @Version: v1.0
 */
@Slf4j
public class TestLock {

    @Test
    public void testLock() throws InterruptedException {
        log.info("test lock");
        Object lock = new Object();
        //lock.wait();
        synchronized (lock) {
            lock.wait();
        }
        log.info("test lock");
    }

    @Test
    public void testThreadPool() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20, 10,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(10));
        threadPoolExecutor.execute(() -> {
            log.info("thread pool");
        });
        threadPoolExecutor.shutdown();
        threadPoolExecutor.shutdownNow();
    }

    @Test
    public void testFuture() {
        CompletableFuture.supplyAsync(() -> "Hello").thenApply((r) -> r + " World").thenAccept(log::info);
        CompletableFuture<String> baseFuture = CompletableFuture.completedFuture("Base Future");
        log.info(baseFuture.thenApply((r) -> r + " Then Apply").join());
        baseFuture.thenAccept(log::info).thenAccept((Void) -> log.info("Void"));
    }


}
